package love.yuanmengrobot.guoyitanghoutai.cotroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import love.yuanmengrobot.guoyitanghoutai.dto.TbNatmehaHotspotAndPage;
import love.yuanmengrobot.guoyitanghoutai.pojo.TbNatmehaHotspot;
import love.yuanmengrobot.guoyitanghoutai.service.TbNatmehaHotspotService;
import love.yuanmengrobot.guoyitanghoutai.util.DataGridView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import love.yuanmengrobot.guoyitanghoutai.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/8/17 19:52
 */
@CrossOrigin
@RestController
public class TbNatmehaHotspotController {
    private static final String SUCCESS = "SUCCESS";
    private static final String FAILED = "FAILED";
    @Autowired
    private final TbNatmehaHotspotService tbNatmehaHotspotService;

    public TbNatmehaHotspotController(TbNatmehaHotspotService tbNatmehaHotspotService) {
        this.tbNatmehaHotspotService = tbNatmehaHotspotService;
    }

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页获取所有的节气养生数据，注意：已废弃使用！
     */
    @GetMapping("/getHealthPreservation")
    public IPage getHealthPreservation(int current, int limit) {

        String key = "getHealthPreservation:" + current + ":" + limit;
        if (redisTemplate.hasKey(key)) {
            IPage<TbNatmehaHotspot> pageCourse2 = new Page<>(current, limit);
            return JsonXMLUtils.json2obj((String) redisTemplate.opsForValue().get(key), pageCourse2.getClass());
        }

        return tbNatmehaHotspotService.selectHealthPreservation(current, limit, "0");
    }

    /**
     * 儿童健康查询
     *
     * @param page 当前页码
     * @return 当前页数据
     */
    @GetMapping("getChildrenHealth")
    public DataGridView getChildrenHealth(int page, int size) {
        return new DataGridView(tbNatmehaHotspotService
                .selectChildrenHealth(new Page<>(page, size)).getRecords());
    }

    /**
     * 新增儿童健康记录
     *
     * @param entity 记录内容
     * @return 成功 OR 失败
     */
    @RequestMapping("addChildrenHealth")
    public String addChildrenHealth(TbNatmehaHotspot entity) {
        return tbNatmehaHotspotService.insertChildrenHealth(entity) ? SUCCESS : FAILED;
    }

    /**
     * 删除儿童健康记录
     *
     * @param itemCode 标识符
     * @return 成功 OR 失败
     */
    @RequestMapping("removeChildrenHealth")
    public String removeChildrenHealth(String itemCode) {
        return tbNatmehaHotspotService.deleteChildrenHealth(itemCode) ? SUCCESS : FAILED;
    }

    /**
     * 修改儿童健康记录
     *
     * @param entity 修改记录内容
     * @return 成功 OR 失败
     */
    @RequestMapping("reviveChildrenHealth")
    public String reviveChildrenHealth(TbNatmehaHotspot entity) {
        return tbNatmehaHotspotService.updateChildrenHealth(entity) ? SUCCESS : FAILED;
    }
    /*
    模糊查找，按照hotspotTitle
     */
    @GetMapping("/getHealthPreservationByHotspotTitle")
    public IPage getHealthPreservationByHotspotTitle(int current, int limit, String hotspotTitle) {
        return tbNatmehaHotspotService.selectHealthPreservationByHotspotTitle(current, limit, "0", hotspotTitle);
    }

    /*
    插入文章：节气养生
     */
    @PostMapping("/insertTbNatmehaHotspotHealthPreservation")
    public ResultObj insertHealthPreservation(@RequestBody TbNatmehaHotspot tbNatmehaHotspot) {

        tbNatmehaHotspot.setItemcode(UUIDUtils.getUUID());
        tbNatmehaHotspot.setItemcreateat(new Date());
        tbNatmehaHotspot.setItemupdateat(new Date());
        tbNatmehaHotspot.setCreater("槐底街道办事处社区卫生服务中心");
        tbNatmehaHotspot.setUserCode("3793889f-9da0-4830-8ed4-9290318c2505");
        tbNatmehaHotspot.setHotspotAuthor("槐底街道办事处社区卫生服务中心");
        tbNatmehaHotspot.setCreater("槐底街道办事处社区卫生服务中心");
        tbNatmehaHotspot.setDataType("0");
//        System.out.println("打印："+tbNatmehaHotspot.getHotspotTitle()+"&"+tbNatmehaHotspot.getHotspotContent()+"$"+tbNatmehaHotspot.getDataStatus());


        try {
            int i = tbNatmehaHotspotService.insertHealthPreservation(tbNatmehaHotspot);
//            System.out.println("插入：" + i);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {

            return ResultObj.ADD_ERROR;
        }
    }

    /*
    更新文章：节气养生
     */
    @PostMapping("/updateTbNatmehaHotspotHealthPreservation")
    public ResultObj updateHealthPreservation(@RequestBody TbNatmehaHotspot tbNatmehaHotspot) {
        try {
            int i = tbNatmehaHotspotService.updateHealthPreservation(tbNatmehaHotspot);
            System.out.println("更新：" + i);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
    }

    /*
    更新文章状态：节气养生
    */
    @GetMapping("/updateTbNatmehaHotspotHealthPreservationStatus")
    public ResultObj updateTbNatmehaHotspotHealthPreservationStatus(@RequestParam(name = "itemcode") String itemcode, @RequestParam(name = "status") String status) {
        try {
            int i = tbNatmehaHotspotService.updateHealthPreservationStatus(itemcode,status);
            System.out.println("更新：" + i);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
    }

    /*
    删除文章：节气养生
    */
    @GetMapping("/deleteTbNatmehaHotspotHealthPreservation")
    public ResultObj deleteHealthPreservation(@RequestParam(name = "itemcode") String itemcode) {
        System.out.println("#" + itemcode);

        try {
            int i = tbNatmehaHotspotService.deleteHealthPreservation(itemcode);
            System.out.println("删除：" + i);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 查看儿童健康记录内容
     *
     * @param itemCode 唯一标识符
     * @return 内容
     */
    @GetMapping("getChildrenHealthContent")
    public String getChildrenHealthContent(String itemCode) {
        return tbNatmehaHotspotService.selectChildrenHeathContent(itemCode);
    }

    @GetMapping("getPages")
    public String getPages(int size) {
        return tbNatmehaHotspotService.selectPageSum(size) + "";
    }

    /*
    获取药膳食疗列表
     */
    @RequestMapping("/getMedicineDiet")
    public Vo getMedicineDiet(Parameters params){
        return tbNatmehaHotspotService.selectMedicineDiet(params);
    }


    /**
     * 查询中医文化
     * @param tbNatmehaHotspotAndPage
     * @return
     */
    @RequestMapping("getAllTraditionalCultural")
    public JsonResult getAllTraditionalCultural(@RequestBody TbNatmehaHotspotAndPage tbNatmehaHotspotAndPage){

        tbNatmehaHotspotAndPage.getTbNatmehaHotspot().setUserCode("6e317f9d-b021-4bfa-b4eb-b4cafbf71364");
        return new JsonResult<>(true,tbNatmehaHotspotService.getAll(tbNatmehaHotspotAndPage));

    }




    @RequestMapping("editTraditionalCultural")
    public JsonResult editTraditionalCultural(@RequestBody TbNatmehaHotspot tbNatmehaHotspot){
        tbNatmehaHotspot.setUpdater("白鹿泉乡卫生院");
        System.out.println(tbNatmehaHotspot);

        if(tbNatmehaHotspotService.editTraditionalCultural(tbNatmehaHotspot)){
            return new JsonResult<>(true);
        }else {
            return new JsonResult<>(false);
        }
    }


    @RequestMapping("deleTraditionalCultural")
    public JsonResult deleTraditionalCultural(@RequestBody TbNatmehaHotspot tbNatmehaHotspot){


        System.out.println("ID"+ tbNatmehaHotspot.getItemid());

        if(tbNatmehaHotspotService.deleTraditionalCultural(tbNatmehaHotspot)){
            return new JsonResult<>(true);
        }else {
            return new JsonResult<>(false);
        }
    }


    @RequestMapping("insertTraditionalCultural")
    public JsonResult insertTraditionalCultural(@RequestBody TbNatmehaHotspot tbNatmehaHotspot){
        tbNatmehaHotspot.setUserCode("6e317f9d-b021-4bfa-b4eb-b4cafbf71364");
        tbNatmehaHotspot.setCreater("白鹿泉乡卫生院");
        System.out.println(tbNatmehaHotspot);

        if(tbNatmehaHotspotService.insertTraditionalCultural(tbNatmehaHotspot)){
            return new JsonResult<>(true);
        }else {
            return new JsonResult<>(false);
        }
    }
}
